﻿using AutoUpdateLib;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Listener_SMS
{
    public class AutoUpdateUtil
    {
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        public static string CheckUpdatesUrl = "http://download.fshqtx.com/listenersms/Listener_SMS.application";

        public static bool Auto(string[] args)
        {
            if (args.Length == 0)
            {
                //判断是否更新成功
                bool AutoUpdateSuccess = AutoUpdate.CheckSuccess(args);
                if (!AutoUpdateSuccess)
                {
                    Process Aup = AutoUpdate.CheckRunning();
                    //判断更新程序是否在运行
                    if (Aup != null)
                    {
                        SwitchToThisWindow(Aup.MainWindowHandle, true);    // 激活，显示在最前
                        return false;
                    }
                    try
                    {
                        //ClickOnce 发布后的 xxx.application http请求路径
                        if (AutoUpdate.CheckUpdates(CheckUpdatesUrl, args))
                        {
                            return false;
                        }
                    }
                    catch (AutoUpdaterFileNotExistException e)
                    {
                        //MessageBox.Show(e.Message, "提示");
                    }
                }
            }
            return true;
        }

        public static bool Auto() {
            return Auto(new string[] { });
        }
    }
}
